<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class GameListCollection extends ResourceCollection
{
    public $resource;

    public function __construct($resource)
    {
        $this->resource = $resource;
        parent::__construct($resource);
    }

    public function toArray($request)
    {
        if ($this->resource->count() >= 1) {
            foreach ($this->collection as $key => $item) {
                $data[$key]['id'] = $item->id;
                $data[$key]['main_game_plat_code'] = $item->main_game_plat_code;
                $data[$key]['cn_name'] = $item->cn_name;
                $data[$key]['en_name'] = $item->en_name;
                $data[$key]['pc_game_code'] = $item->pc_game_code;
                $data[$key]['pc_game_deputy_code'] = $item->pc_game_deputy_code;
                $data[$key]['mobile_game_code'] = $item->mobile_game_code;
                $data[$key]['mobile_game_deputy_code'] = $item->mobile_game_deputy_code;
                $data[$key]['record_match_code'] = $item->record_match_code;
                $data[$key]['record_match_deputy_code'] = $item->record_match_deputy_code;
                $data[$key]['img1'] = $item->getImg1Url();
                $data[$key]['img2'] = $item->getImg2Url();
                $data[$key]['type'] = $item->gameType->name ?? '';
                $data[$key]['category'] = $item->gameCategory->name ?? '';
                $data[$key]['line_num'] = $item->gameLine->name ?? '';
                $data[$key]['money_return_rate'] = $item->money_return_rate;
                $data[$key]['bonus_pool'] = $item->bonus_pool;
                $data[$key]['able_demo'] = $item->able_demo;
                $data[$key]['is_lobby'] = $item->is_lobby;
                $data[$key]['created_at'] = $item->created_at->toDateTimeString();
            }
            $this->resource->setCollection(collect($data));
            return [
                $this->resource
            ];
        } else {
            return [];
        }

    }
}
